package com.tushu.view.admin;

import com.tushu.dao.BookDao;
import com.tushu.model.Book;
import com.tushu.utils.DataUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BookManage extends JPanel {
    private JTextField jt1;
    private JTable table;
    private JTextField jt2;
    private JTextField jt3;
    private JTextField jt4;
    private JTextField jt5;
    private JTextField jt6;

    private BookDao bookDao = new BookDao();

    String sql = "select * from tb_book";
    String sql2 = "select * from tb_book where book_name=?";
    String sql3 = "select * from tb_book where isbn=?";

    private String[] table_header = {"序号", "id", "ISBN", "书名", "作者", "出版社", "库存"};

    /**
     * Create the panel.
     */
    public BookManage() {
        setLayout(null);

        JButton jb_find = new JButton("\u67E5\u8BE2");

        jb_find.setBounds(288, 10, 77, 23);
        add(jb_find);

        JButton jb_reset = new JButton("\u91CD\u7F6E");
        jb_reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jt1.setText("");
                getAll(sql, null);
            }
        });
        jb_reset.setBounds(375, 10, 77, 23);
        add(jb_reset);

        jt1 = new JTextField();
        jt1.setBounds(99, 11, 179, 21);
        add(jt1);
        jt1.setColumns(10);

        JRadioButton rb_isbn = new JRadioButton("ISBN", true);
        rb_isbn.setBounds(109, 38, 66, 23);
        add(rb_isbn);

        JRadioButton rb_name = new JRadioButton("\u4E66\u540D");
        rb_name.setBounds(194, 39, 127, 23);
        add(rb_name);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rb_isbn);
        buttonGroup.add(rb_name);

        jb_find.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = jt1.getText();
                if (rb_isbn.isSelected()) {
                    getAll(sql3, text);
                }
                if (rb_name.isSelected()) {
                    getAll(sql2, text);
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 67, 528, 248);
        add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        JLabel lblNewLabel = new JLabel("ISBN");
        lblNewLabel.setBounds(34, 320, 58, 15);
        add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u4E66\u540D");
        lblNewLabel_1.setBounds(189, 323, 58, 15);
        add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("\u4F5C\u8005");
        lblNewLabel_2.setBounds(34, 343, 58, 15);
        add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("\u51FA\u7248\u793E");
        lblNewLabel_3.setBounds(362, 323, 58, 15);
        add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("\u6570\u91CF");
        lblNewLabel_4.setBounds(194, 346, 58, 15);
        add(lblNewLabel_4);

        JButton jb_add = new JButton("\u65B0\u589E");
        jb_add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String isbn = jt2.getText();
                String name = jt3.getText();
                String publisher = jt4.getText();
                String author = jt5.getText();
                int num = Integer.parseInt(jt6.getText());
                Book bookByIsbn = bookDao.getBookByIsbn(isbn);
                if (bookByIsbn == null) {
                    Book book = new Book(0, isbn, name, publisher, author, num);
                    int res = bookDao.addBook(book);
                    if (res > 0) {
                        JOptionPane.showMessageDialog(null, "添加《" + name + "》成功！");
                        getAll(sql, null);
                    } else {
                        JOptionPane.showMessageDialog(null, "添加《" + name + "》失败！");
                    }
                } else {
                    num = bookByIsbn.getNum() + num;
                    int row = table.getSelectedRow();
                    int id = (int) table.getValueAt(row, 1);
                    Book book = new Book(id, isbn, name, publisher, author, num);
                    int res = bookDao.updateBook(book);
                    if (res > 0) {
                        JOptionPane.showMessageDialog(null, "更新《" + name + "》库存成功！");
                        getAll(sql, null);
                    } else {
                        JOptionPane.showMessageDialog(null, "更新《" + name + "》库存失败！");
                    }
                }
            }
        });
        jb_add.setBounds(102, 371, 77, 23);
        add(jb_add);

        JButton jb_update = new JButton("\u4FEE\u6539");
        jb_update.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String isbn = jt2.getText();
                String name = jt3.getText();
                String publisher = jt4.getText();
                String author = jt5.getText();
                int num = Integer.parseInt(jt6.getText());
                int row = table.getSelectedRow();
                int id = (int) table.getValueAt(row, 1);
                Book book = new Book(id, isbn, name, publisher, author, num);
                int res = bookDao.updateBook(book);
                if (res > 0) {
                    JOptionPane.showMessageDialog(null, "更新《" + name + "》库存成功！");
                    getAll(sql, null);
                } else {
                    JOptionPane.showMessageDialog(null, "更新《" + name + "》库存失败！");
                }
            }
        });
        jb_update.setBounds(239, 371, 77, 23);
        add(jb_update);


        JButton jb_delete = new JButton("\u5220\u9664");
        jb_delete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                int id = (int) table.getValueAt(row, 1);
                String book_name = table.getValueAt(row, 3).toString();
                int res = bookDao.deleteBook(id);
                if (res > 0) {
                    JOptionPane.showMessageDialog(null, "删除《" + book_name + "》成功！");
                    getAll(sql, null);
                } else {
                    JOptionPane.showMessageDialog(null, "删除《" + book_name + "》失败！");
                }
            }
        });
        jb_delete.setBounds(362, 371, 77, 23);
        add(jb_delete);

        jt2 = new JTextField();
        jt2.setBounds(69, 320, 110, 21);
        add(jt2);
        jt2.setColumns(10);

        jt3 = new JTextField();
        jt3.setColumns(10);
        jt3.setBounds(228, 320, 111, 21);
        add(jt3);

        jt4 = new JTextField();
        jt4.setColumns(10);
        jt4.setBounds(69, 343, 110, 21);
        add(jt4);

        jt5 = new JTextField();
        jt5.setColumns(10);
        jt5.setBounds(401, 320, 95, 21);
        add(jt5);

        jt6 = new JTextField();
        jt6.setColumns(10);
        jt6.setBounds(229, 343, 110, 21);
        add(jt6);
        getAll(sql, null);

        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int row = table.getSelectedRow();
                jt2.setText((String) table.getValueAt(row, 2));
                jt3.setText((String) table.getValueAt(row, 3));
                jt4.setText((String) table.getValueAt(row, 4));
                jt5.setText((String) table.getValueAt(row, 5));
                jt6.setText(String.valueOf(table.getValueAt(row, 6)));
            }
        });
    }

    public void getAll(String sql, String text) {
        if (text == null || text.equals("")) {
            try {
                ResultSet set = DataUtils.query(sql, new Object[]{});
                showAtTable(set);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                ResultSet set = DataUtils.query(sql, new Object[]{text});
                showAtTable(set);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }


    }

    public void showAtTable(ResultSet set) {
        Object[][] arrays = new Object[0][];
        try {
            arrays = DataUtils.getSetArrays(set);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        DefaultTableModel model = new DefaultTableModel(arrays, table_header);
        table.setModel(model);
    }


}
